package com.huaao.micropolice.core.service.system.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.hibernate.annotations.Parent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.druid.util.StringUtils;
import com.huaao.micropolice.core.common.page.Page;
import com.huaao.micropolice.core.common.page.Sort;
import com.huaao.micropolice.core.model.system.Area;
import com.huaao.micropolice.core.model.system.Dept;
import com.huaao.micropolice.core.repository.system.AreaDao;
import com.huaao.micropolice.core.repository.system.DeptDao;
import com.huaao.micropolice.core.service.system.AreaService;

@Service
@Transactional
public class AreaServiceImpl implements AreaService{
	
	@Autowired
	private AreaDao areaDao;
	@Autowired
	private DeptDao deptDao;
	
	@Override
	public String save(Area obj) {
		Area parent = (Area) areaDao.load(Area.class, obj.getParentId());
		obj.setLevel(parent.getLevel()+1);
		obj.setIsLeaf(Area.LEAF_YES);
		parent.setIsLeaf(Area.LEAF_NO);
		areaDao.save(parent);
		return areaDao.save(obj);
	}

	@Override
	public List<Area> findByAreaname(String name) {
		return null;
	}

	@Override
	public List<Area> findByAreaParentId(String id) {
		// TODO Auto-generated method stub
		if(StringUtils.isEmpty(id)){
			List<Area> list = new ArrayList();
			list.add((Area) areaDao.load(Area.class,"0"));
			list.addAll(areaDao.findByAreaParentId("0"));
			return list;
		}else{
			return areaDao.findByAreaParentId(id);
		}
		
	}

	@Override
	public void update(Area obj) {
		Area no = (Area)areaDao.load(Area.class, obj.getId());
		try {
			if(no.getParentId().equals(obj.getParentId())){
				no.setName(obj.getName());
				no.setCode(obj.getCode());
				areaDao.update(no);
			}else{
				//检查原来父节点是否变为子节点
				Area oldParent = (Area)areaDao.get(Area.class, no.getParentId());
				no.setName(obj.getName());
				no.setCode(obj.getCode());
				no.setParentId(obj.getParentId());
				areaDao.update(no);
				if(oldParent.getChildren().size() <= 1){
					oldParent.setIsLeaf(Area.LEAF_YES);
					areaDao.update(oldParent);
				}
				
				//检查是否需要更新LEVEL。如需要则更改其以下节点的LEVEL
				Area newParent = (Area)areaDao.get(Area.class, obj.getParentId());
				if(oldParent.getLevel() != newParent.getLevel()){
					this.updateLevel(no);
				}
				//新的父节点需要更新is_leaf
				if(Area.LEAF_YES.equals(newParent.getIsLeaf()) ){
					newParent.setIsLeaf(Area.LEAF_NO);
					deptDao.update(newParent);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void updateLevel(Area area){
		Area p = (Area)areaDao.load(Area.class, area.getParentId());
		area.setLevel(p.getLevel()+1);
		areaDao.update(area);
		for(Area item : area.getChildren()){
			this.updateLevel(item);
		}
	}
	@Override
	public void del(String id) throws Exception {
		Area obj = (Area)areaDao.load(Area.class,id);
		delArea(obj);
		//检查父节点是否需要更新is_leaf
		Area p = (Area)areaDao.load(Area.class,obj.getParentId());
		if(p.getChildren().size() <= 1){
			p.setIsLeaf(Area.LEAF_YES);
			areaDao.update(p);
		}
		
	}

	private void delArea(Area area) throws Exception{
		Set<Area> list = area.getChildren();
		for(Area item : list){
			delArea(item);
		}
		if( !deptDao.findByAreaId(area.getId()).isEmpty() ){
			throw new Exception("["+area.getName()+"] 存在关联单位，无法删除");
		}
		areaDao.deleteById(Area.class, area.getId());
	}
	 
	@Override
	public List<Area> findAll() {
		List<Area> list = areaDao.findAll();
		List<Area> destList = new ArrayList<Area>();
		sortList(destList,list,null);
		return destList;
	}
	
	private void sortList(List<Area> destList, List<Area> sourceList, String parentId) {
		for (int i=0; i<sourceList.size(); i++){
			Area e = sourceList.get(i);
			if((e.getParent() == null && parentId==null) || (e.getParent() != null && e.getParent().getId().equals(parentId))){
				if(!destList.contains(e)){
					destList.add(e);
				}
				sortList(destList,sourceList,e.getId());
			}
		}
	} 
}
